<?php 
/**
 * Search foods
 */
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

if(isset($_GET['search_food_name'])){
	$search = $_GET['search_food_name'];
	//mysql_query("set names 'utf8'");
	$sql = "SELECT * FROM MON_AN WHERE TEN_MON_AN LIKE '%".$search."%' ";
	$result = mysql_query($sql);
	
	if(mysql_num_rows($result) > 0){
		$response["foods"] = array();
		while($row = mysql_fetch_array($result)){
			$food = array();
			$food["id"] = $row["id"];
			$food["TEN_MON_AN"] = $row["TEN_MON_AN"];
	        $food["DIA_CHI"] = $row["DIA_CHI"];
	        $food["THANH_PHO"] = $row["THANH_PHO"];
	        $food["SO_DIEN_THOAI"] = $row["SO_DIEN_THOAI"];
	        $food["MO_TA"] = $row["MO_TA"];
	        $food["LATITUDE"] = $row["LATITUDE"];
	        $food["LONGIUDE"] = $row["LONGIUDE"];
	        // push single food into final response array
	        array_push($response["foods"], $food);
		}
		
		// success
		$response["success"] = 1;
		// echoing JSON response
		echo utf8_encode(json_encode($response));
	}
	else {
		// no foods found
		$response["success"] = 0;
		$response["message"] = "No foods found";
	
		// echo no users JSON
		print utf8_encode(json_encode($response));
	}
}
else if(isset($_GET["search_food_address"])){
	$search = $_GET["search_food_address"];

	$sql = "SELECT * FROM MON_AN WHERE DIA_CHI LIKE '%".$search."%' OR THANH_PHO LIKE '%".$search."%' ";
	$result = mysql_query($sql);
	
	if(mysql_num_rows($result) > 0){
		$response["foods"] = array();
		while($row = mysql_fetch_array($result)){
			$food = array();
			$food["id"] = $row["id"];
			$food["TEN_MON_AN"] = $row["TEN_MON_AN"];
			$food["DIA_CHI"] = $row["DIA_CHI"];
			$food["THANH_PHO"] = $row["THANH_PHO"];
			$food["SO_DIEN_THOAI"] = $row["SO_DIEN_THOAI"];
			$food["MO_TA"] = $row["MO_TA"];
			$food["LATITUDE"] = $row["LATITUDE"];
			$food["LONGIUDE"] = $row["LONGIUDE"];
			// push single food into final response array
			array_push($response["foods"], $food);
		}
	
		// success
		$response["success"] = 1;
		// echoing JSON response
		echo utf8_encode(json_encode($response));
	}else {
		// no foods found
		$response["success"] = 0;
		$response["message"] = "No foods found";
	
		// echo no users JSON
		print utf8_encode(json_encode($response));
	}
}
else {
	// required field is missing
	$response["success"] = -1;
	$response["message"] = "Required field(s) is missing";

	// echoing JSON response
	echo utf8_encode(json_encode($response));
}
?>